package com.cookie.meetproject.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cookie.framework.adapter.CommonAdapter;
import com.cookie.framework.adapter.CommonViewHolder;
import com.cookie.framework.base.BaseUIActivity;
import com.cookie.framework.bmob.BmobManger;
import com.cookie.framework.bmob.Friend;
import com.cookie.framework.bmob.IMUser;
import com.cookie.framework.cloud.CloudManager;
import com.cookie.framework.entity.Constants;
import com.cookie.framework.helper.GlideHelper;
import com.cookie.framework.manager.DialogManager;
import com.cookie.framework.utils.CommonUtils;
import com.cookie.framework.view.DialogView;
import com.cookie.meetproject.R;
import com.cookie.meetproject.model.UserInfoModel;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class UserInfoActivity extends BaseUIActivity implements View.OnClickListener {

    /**
     * 1.根据传递过来的ID 查询用户信息并且显示
     * -普通的信息
     * -构建一个Recycleview
     * 2.建立好友关系模型
     * 3.实现添加好友的提示框
     * 4.发送添加好友的消息
     * 5.接收好友的消息
     */

    public static void startActivity(Context context, String userId) {
        Intent intent = new Intent(context, UserInfoActivity.class);
        intent.putExtra(Constants.INTENT_USER_ID, userId);
        context.startActivity(intent);
    }

    private RelativeLayout llBack;
    private CircleImageView ivUserPhoto;
    private TextView tvNickname;
    private TextView tvDesc;
    private RecyclerView mUserInfoView;
    private Button btnAddFriend;
    private Button btnChat;
    private Button btnAudioChat;
    private Button btnVideoChat;
    private LinearLayout llIsFriend;

    private String userId = "";
    private CommonAdapter<UserInfoModel> userInfoModelCommonAdapter;
    private List<UserInfoModel> list = new ArrayList<>();
    //个人信息颜色
    private int[] mColor = {0x881E90FF, 0x8800FF7F, 0x88FFD700, 0x88FF6347, 0x88F08080, 0x8840E0D0};

    private DialogView addFriendDialogView;
    private EditText etMsg;
    private TextView tvCancel;
    private TextView tvAddFriend;
    private IMUser imUser;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initView();
    }

    private void initView() {
        initAddFriendDialog();
        userId = getIntent().getStringExtra(Constants.INTENT_USER_ID);

        llBack = (RelativeLayout) findViewById(R.id.ll_back);
        ivUserPhoto = (CircleImageView) findViewById(R.id.iv_user_photo);
        tvNickname = (TextView) findViewById(R.id.tv_nickname);
        tvDesc = (TextView) findViewById(R.id.tv_desc);
        mUserInfoView = (RecyclerView) findViewById(R.id.mUserInfoView);
        btnAddFriend = (Button) findViewById(R.id.btn_add_friend);
        btnChat = (Button) findViewById(R.id.btn_chat);
        btnAudioChat = (Button) findViewById(R.id.btn_audio_chat);
        btnVideoChat = (Button) findViewById(R.id.btn_video_chat);
        llIsFriend = (LinearLayout) findViewById(R.id.ll_is_friend);

        btnAddFriend.setOnClickListener(this);
        btnChat.setOnClickListener(this);
        btnAudioChat.setOnClickListener(this);
        btnVideoChat.setOnClickListener(this);
        llBack.setOnClickListener(this);
        userInfoModelCommonAdapter = new CommonAdapter<>(list, new CommonAdapter.OnBindDataListener<UserInfoModel>() {
            @Override
            public void onBindViewHolder(UserInfoModel model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setText(R.id.tv_type, model.getTitle());
                viewHolder.setText(R.id.tv_content, model.getContent());
                //viewHolder.setBackgroundColor(R.id.ll_bg,model.getBgColor());
                viewHolder.getView(R.id.ll_bg).setBackgroundColor(model.getBgColor());
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.layout_user_info_item;
            }
        });
        mUserInfoView.setLayoutManager(new GridLayoutManager(UserInfoActivity.this, 3));
        mUserInfoView.setAdapter(userInfoModelCommonAdapter);
        queryUserInfo();
    }

    //添加好友的提示框
    private void initAddFriendDialog() {
        addFriendDialogView = DialogManager.getInstance()
                .initView(this, R.layout.dialog_send_friend);
        etMsg = addFriendDialogView.findViewById(R.id.et_msg);
        tvCancel = addFriendDialogView.findViewById(R.id.tv_cancel);
        tvAddFriend = addFriendDialogView.findViewById(R.id.tv_add_friend);

        tvCancel.setOnClickListener(this);
        tvAddFriend.setOnClickListener(this);
    }

    //查询用户信息
    private void queryUserInfo() {
        if (TextUtils.isEmpty(userId)) {
            return;
        }
        //查询用户信息
        BmobManger.getInstance().queryObjectIdUser(userId, new FindListener<IMUser>() {
            @Override
            public void done(List<IMUser> list, BmobException e) {
                if (e == null) {
                    if (CommonUtils.isEmpty(list)) {
                        imUser = list.get(0);
                        updateUserInfo(imUser);
                    }
                }
            }
        });
        //判断好友关系
        BmobManger.getInstance().queryMyFriends(new FindListener<Friend>() {
            @Override
            public void done(List<Friend> list, BmobException e) {
                if (e == null) {
                    if (CommonUtils.isEmpty(list)) {
                        //你有一个好友列表
                        for (int i = 0; i < list.size(); i++) {
                            Friend friend = list.get(i);
                            //判断这个对象中的ID是否跟我目前的userID相同
                            if (friend.getFriendUser().getObjectId().equals(userId)) {
                                //你们是好友关系
                                btnAddFriend.setVisibility(View.GONE);
                                llIsFriend.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }
            }
        });
    }

    //更新用户信息
    private void updateUserInfo(IMUser imUser) {
        //设置基本属性
        GlideHelper.loadUrl(UserInfoActivity.this, imUser.getPhoto(), ivUserPhoto);
        tvNickname.setText(imUser.getNickName());
        tvDesc.setText(imUser.getDesc());
        //性别 年龄 生日 星座 爱好 单身状态
        addUserInfoModel(mColor[0], getString(R.string.text_me_info_sex), imUser.isSex() ? getString(R.string.text_me_info_boy) : getString(R.string.text_me_info_girl));
        addUserInfoModel(mColor[1], getString(R.string.text_me_info_age), imUser.getAge() + getString(R.string.text_search_age));
        addUserInfoModel(mColor[2], getString(R.string.text_me_info_birthday), imUser.getBirthday());
        addUserInfoModel(mColor[3], getString(R.string.text_me_info_constellation), imUser.getConstellation());
        addUserInfoModel(mColor[4], getString(R.string.text_me_info_hobby), imUser.getHobby());
        addUserInfoModel(mColor[5], getString(R.string.text_me_info_status), imUser.getStatus());
        //刷新数据
        userInfoModelCommonAdapter.notifyDataSetChanged();
    }

    //添加数据
    private void addUserInfoModel(int color, String title, String content) {
        UserInfoModel userInfoModel = new UserInfoModel();
        userInfoModel.setBgColor(color);
        userInfoModel.setTitle(title);
        userInfoModel.setContent(content);
        list.add(userInfoModel);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_add_friend:
                String msg = etMsg.getText().toString().trim();
                if (TextUtils.isEmpty(msg)) {
                    msg = "你好,我是" + BmobManger.getInstance().getUser().getNickName();
                }
                CloudManager.getInstance().sendTextMessage(msg, CloudManager.TYPE_ADD_FRIEND, userId);
                DialogManager.getInstance().hide(addFriendDialogView);
                Toast.makeText(this, "消息发送成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_cancel:
                DialogManager.getInstance().hide(addFriendDialogView);
                break;
            case R.id.btn_add_friend:
                DialogManager.getInstance().show(addFriendDialogView);
                break;
            case R.id.btn_chat:
                ChatActivity.startActivity(UserInfoActivity.this, userId, imUser.getNickName()
                        , imUser.getPhoto());
                break;
            case R.id.btn_audio_chat:
                if (!checkWindowPermissions()){
                    requestWindowPermissions();
                }else {
                    CloudManager.getInstance().startAudioCall(this,userId);
                }
                break;
            case R.id.btn_video_chat:
                if (!checkWindowPermissions()){
                    requestWindowPermissions();
                }else {
                    CloudManager.getInstance().startVideoCall(this,userId);
                }
                break;
            case R.id.ll_back:
                UserInfoActivity.this.finish();
                break;
        }
    }
}
